#include "project.h"

#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QDebug>

Project::Project(const QString &dirPath, const QString &name)
    : m_dirPath{dirPath}
    , m_name{name}
    , m_configDirPath(dirPath+"/"+name)
    , m_projectName(name)
{
}

bool Project::isValid()
{
    return !m_dirPath.isEmpty() && !m_name.isEmpty();
}

QString Project::dirPath() const
{
    return m_dirPath;
}

void Project::setDirPath(const QString &newDirPath)
{
    m_dirPath = newDirPath;
}

bool Project::create()
{
    QDir dir;
    if(!dir.exists(m_configDirPath)) {
        if(!dir.mkpath(m_configDirPath)) {
            qDebug() << "create dir path fail. " << m_configDirPath;
            return false;
        }
    }
    else {
        qDebug() << "config dir path existed. " << m_configDirPath;
        return false;
    }

    QFile file(configFileName());
    if(!file.open(QIODevice::WriteOnly)) {
        qDebug() << "create config file fail. " << configFileName();
        return false;
    }

    QJsonObject root;
    root["ProjectName"] = name();
    QJsonArray ja;
    root["Tables"] = ja;
    QJsonDocument doc(root);
    file.write(doc.toJson());
    file.close();
    return true;
}

bool Project::load(const QString &configFilePath)
{
    QFileInfo info(configFilePath);
    if(!info.exists()) {
        qDebug() << "project config file not exist. " << configFilePath;
        return false;
    }
    setName(info.baseName());
    setConfigDirPath(info.absolutePath());
    QDir dir(configDirPath());
    dir.cdUp();
    setDirPath(dir.absolutePath());

    QFile file(configFilePath);
    if(!file.open(QIODevice::ReadOnly)) {
        qDebug() << "project config file open fail. " << configFilePath;
        return false;
    }

    QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
    QJsonObject root = doc.object();
    if(!root.contains("ProjectName")) {
        setProjectName(name());
    }
    else {
        setProjectName(root["ProjectName"].toString());
    }

    if(root.contains("Tables") && root["Tables"].isArray()) {
        auto tables = root["Tables"].toArray();

        for (int i  = 0; i < tables.size(); ++i) {
            addTableName(tables.at(i).toString());
        }
    }

    file.close();
    qDebug() << "Load project success. " << configFilePath << root << m_tableFileNames;
    return true;
}

QString Project::name() const
{
    return m_name;
}

void Project::setName(const QString &newName)
{
    m_name = newName;
}

QString Project::configFileName()
{
    return configDirPath() + "/" + name() + ProjectFileSuffixName;
}

QString Project::tableDirPath()
{
    return configDirPath() + "/" + TableFolderName;
}

QString Project::configDirPath() const
{
    return m_configDirPath;
}

void Project::setConfigDirPath(const QString &newConfigDirPath)
{
    m_configDirPath = newConfigDirPath;
}

QString Project::projectName() const
{
    return m_projectName;
}

void Project::setProjectName(const QString &newProjectName)
{
    m_projectName = newProjectName;
}

void Project::addTableName(const QString &newTableName)
{
    if(QFile::exists(tableDirPath() + "/" + newTableName))
        m_tableFileNames.push_back(newTableName);
}
